package dk.aau.walkietalkieui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class Base_FragmentActivity extends FragmentActivity {
	private static final int RESULT_SETTINGS = 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.action_settings:
			Intent i = new Intent(this, SettingsActivity.class);
			startActivityForResult(i, RESULT_SETTINGS);
			break;

		case R.id.action_refresh:

			SharedPreferences sp = PreferenceManager
					.getDefaultSharedPreferences(getApplicationContext());
			String name = sp.getString("prefUsername", "unknown");
			Toast.makeText(getApplicationContext(), name,
					Toast.LENGTH_SHORT).show();
			finish();
			break;
		case R.id.action_help:
			// Toast.makeText(getApplicationContext(), "Help me!",
			// Toast.LENGTH_SHORT).show();
			showPopupWindow(R.layout.popup_help, R.id.btnPopUpOkHelp);
			break;
		case R.id.action_about:
			showPopupWindow(R.layout.popup_about, R.id.btnPopUpOkAbout);
			// Toast.makeText(getApplicationContext(), "Designed by us",
			// Toast.LENGTH_SHORT).show();
			break;
		}

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		SharedPreferences sharedPref = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		Boolean syncConnPref = sharedPref.getBoolean(
				SettingsActivity.KEY_PREF_SYNC_CONN, false);

		MenuItem actionStatus = menu.findItem(R.id.action_status);

		if (syncConnPref) {
			actionStatus.setTitle("Online");

		} else {

			actionStatus.setTitle("Offline");
		}

		return super.onPrepareOptionsMenu(menu);
	}

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		super.onResume();
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		// deal with the compatibility
		if (currentapiVersion > android.os.Build.VERSION_CODES.HONEYCOMB) {
			invalidateOptionsMenu();
		}

	}

	public void showPopupWindow(int resource, int buttonId) {
		LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View popupView = layoutInflater.inflate(resource, null);
		final PopupWindow popupWindow = new PopupWindow(popupView,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

		// Button in the popUp window
		Button btnDismiss = (Button) popupView.findViewById(buttonId);
		btnDismiss.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});

		// To find the current view
		View v = findViewById(R.id.btnSend);

		if (v != null) { // if there is a button in the view i.e. ChatWindow
			popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

		} else {
			// if there is a text, i.e. the contact list
			v = findViewById(R.id.Text);
			popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

		}

	}
}
